global !p

from mdtex.scopes import inline_math, display_math
from mdtex.extcal import *

endglobal

context "inline_math()"
snippet sym "SymPy Block" w
\begin{sympy} ${1:${VISUAL}} \end{sympy}$0
endsnippet

context "display_math()"
snippet sym "SymPy Block" b
\begin{sympy}
${1:${VISUAL}}
\end{sympy}$0
endsnippet

context "inline_math()"
snippet wlf "WolframScript Block" w
\begin{wolfram} ${1:${VISUAL}} \end{wolfram}$0
endsnippet

context "display_math()"
snippet wlf "WolframScript Block" b
\begin{wolfram}
${1:${VISUAL}}
\end{wolfram}$0
endsnippet

priority 1
context "inline_math()"
snippet \sym "SymPy Block" w
\begin{latex_sympy} ${1:${VISUAL}} \end{latex_sympy}$0
endsnippet

context "display_math()"
snippet \sym "SymPy Block" b
\begin{latex_sympy}
${1:${VISUAL}}
\end{latex_sympy}$0
endsnippet

context "inline_math()"
snippet \wlf "WolframScript Block" w
\begin{latex_wolfram} ${1:${VISUAL}} \end{latex_wolfram}$0
endsnippet

context "display_math()"
snippet \wlf "WolframScript Block" b
\begin{latex_wolfram}
${1:${VISUAL}}
\end{latex_wolfram}$0
endsnippet

context "inline_math()"
snippet '\\begin\{sympy\} (.*?) \\end\{sympy\}' "Evaluate SymPy" wr
`!p
from sympy import *
x, y, z, t = symbols('x y z t')
i, j, k = symbols('i j k', integer=True)
f, g, h = symbols('f g h', cls=Function)
result = latex(eval(pre_process_text(match.group(1))))
snip.rv = process_latex(result)
`
endsnippet

context "get_environment('sympy', snip)"
pre_expand "calculate_sympy(snip)"
snippet \end{sympy} "Evaluate Sympy" b
endsnippet

# context "inline_math()"
# snippet '\\sympy (.*?) \\sympy' "Evaluate SymPy" wr
# `!p
# x, y, z, t = symbols('x y z t')
# i, j, k = symbols('i j k', integer=True)
# f, g, h = symbols('f g h', cls=Function)
# result = latex(
#     eval(
#         process_sympy(
#             match.group(1).replace('\\e ', 'e ') \
#                           .replace('\\d ', '\, d')
#         ).replace('\\', '') \
#          .replace('^', '**') \
#          .replace('{', '(') \
#          .replace('}', ')')
#     )
# )
# result = sub(r'(\s|\W?)e(?=\W)', r'\g<1>\\e', result) # 改 e 样式
# result = result.replace(r'\, d', r'\d ') # 改微分算子
# snip.rv = result
# `
# endsnippet

# context "get_environment('sympy', snip)"
# pre_expand "calculate_sympy(snip, True)"
# snippet \\sympy "Evaluate Sympy" b
# endsnippet

context "inline_math()"
snippet '\\begin\{wolfram\} (.*?) \\end\{wolfram\}(\d{,2})' "Evaluate WolframScript" wr
`!p
code = 'ToString[{0}, TeXForm]'.format(match.group(1))
try:
    result = check_output(['wolframscript', '-code', code], \
                          encoding = 'utf-8', \
                          # creationflags = 0x08000000,\
                          timeout  = int(match.group(2) or wolframscript_timeout_default)).strip()
except TimeoutExpired:
    result = ''
snip.rv = process_latex(result)
`
endsnippet

context "get_environment('wolfram', snip)"
pre_expand "calculate_wolfram(snip, False, match.group(1))"
snippet '^\s*\\end\{wolfram\}(\d{,2})' "Evaluate WolframScript" r
endsnippet

context "inline_math()"
snippet '\\begin\{latex_wolfram\} (.*?) \\end\{latex_wolfram\}(\d{,2})' "Evaluate WolframScript" wr
`!p
code = 'ToString[ToExpression["{0}", TeXForm], TeXForm]'.format(pre_process_text(match.group(1)))
try:
    result = check_output(['wolframscript', '-code', code], \
                          encoding      = 'utf-8', \
                          # creationflags = 0x08000000,\
                          timeout       = int(match.group(2) or wolframscript_timeout_default)).strip()
except TimeoutExpired:
    result = ''
snip.rv = process_latex(result)
`
endsnippet

context "get_environment('latex_wolfram', snip)"
pre_expand "calculate_wolfram(snip, True, match.group(1))"
snippet '^\s*\\end{latex_wolfram}(\d{,2})' "Evaluate WolframScript" r
endsnippet
